<?php

class PortalWebServiceDataSearch extends PortalWebServiceDataBase{
	protected function __get_query($query,array $fields){
		return $query ; 
		if(preg_match('/^[a-zA-Z_]+:/',$query)){ // match search
			return $query;
		}else{                                  // open search
			return implode(":$query OR ",$fields).":$query";
		}
	}

	public function _programmes($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json', $extra = array(), $raw=false ){
		//$sort  = array();
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array("meta_keywords","meta_description","title_$lang","premium_title_$lang"));
		if ( !empty( $extra) || $raw) return $this->getDataByQuery ( 'Programme', $query, $offset, $limit, $sort, $extra, $raw ) ;

		return $this->getFormatDataByQuery('Programme',$query,$offset,$limit,$sort,$lang,$format);
		//$data  = $this->getFormatDataByQuery('Programme',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _episodes($query,$offset=0,$limit=10,array $sort=array(),$lang='zh',$format='json'){
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array("description_$lang","title_$lang","premium_title_$lang"));
		
		return $this->getFormatDataByQuery('Episode',$query,$offset,$limit,$sort,$lang,$format);
		//$data  = $this->getFormatDataByQuery('Episode',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _chapters($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json'){
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array("description_$lang","title_$lang","premium_title_$lang"));
		return $this->getFormatDataByQuery('Chapter',$query,$offset,$limit,$sort,$lang,$format);
		//$data  = $this->getFormatDataByQuery('Chapter',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	//public function _videos($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json'){
	public function _videos($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json', $extra = array(), $raw=false ){
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array("title_$lang","premium_title_$lang"));
		if ( !empty( $extra) || $raw) return $this->getDataByQuery ( 'Video', $query, $offset, $limit, $sort, $extra, $raw ) ;

		$data = $this->getFormatDataByQuery('Video',$query,$offset,$limit,$sort,$lang,$format);
		//file_put_contents('/tmp/video.html', var_export($limit,true));	
		return $data;
		//$data  = $this->getFormatDataByQuery('Video',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _categories($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json'){
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array("title_$lang"));
		return $this->getFormatDataByQuery('Category',$query,$offset,$limit,$sort,$lang,$format);
		//$data  = $this->getFormatDataByQuery('Category',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _infos($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json', $extra = array(), $raw=false ){
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array("premium_title_$lang")) ;
		if ( !empty( $extra) || $raw) return $this->getDataByQuery ( 'Info', $query, $offset, $limit, $sort, $extra, $raw ) ;
		

		return $this->getFormatDataByQuery('Info',$query,$offset,$limit,$sort,$lang,$format) ;
		//$data  = $this->getFormatDataByQuery('Info',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}

	public function _all($query,$offset=0,$limit=10,array $sort=array(),$lang='zh', $format='json', $extra= array(),$raw=false){
		$lang  = $this->__check_lang($lang);
		$query = $this->__get_query($query,array()) ;
		if ( !empty( $extra) || $raw) return $this->getDataByQuery ( 'ChapterVideoEpisode', $query, $offset, $limit, $sort, $extra, $raw ) ;

		return $this->getFormatDataByQuery('ChapterVideoEpisode',$query,$offset,$limit,$sort,$lang,$format);
		//$data  = $this->getFormatDataByQuery('Chapter',$query,$offset,$limit,$sort,$lang,$format);
		//unset($data['sort']);
		//return ($format=='xml' ? CommonUtils::arrayToXml($data) : $data);
	}
}
